#include "sgl_draw_font.h"


void sgl_draw_font(sgl_surf_t *surf, char ch, int x, int y, sgl_color_t color, sgl_color_t bg_color, sgl_font_t *font)
{
    int ch_index = (uint8_t)ch - 32;
    const uint8_t *dot = &font->bitmap[font->table[ch_index].bitmap_index];
    for(int i = 0; i < font->table[ch_index].height; i++) {
        for(int j = 0; j < font->table[ch_index].width; j +=2) {
            sgl_draw_point(surf, x + j + font->table[ch_index].ofs_x, y + i + font->table[ch_index].ofs_y, sgl_color_mixer(color, bg_color, (((*dot)>>4)&0xF)*16));
            sgl_draw_point(surf, x + j + font->table[ch_index].ofs_x + 1, y + i + font->table[ch_index].ofs_y, sgl_color_mixer(color, bg_color, (((*dot))&0xF)*16));
            dot ++;
        }
    }
}

void sgl_draw_font_string(sgl_surf_t *surf, const char *string, int x, int y, sgl_color_t color, sgl_color_t bg_color, sgl_font_t *font)
{
    int ofs_x = x;
    int ch_index;
    while(*string) {
        ch_index = (uint8_t)(*string) - 32;
        sgl_draw_font(surf, *string, ofs_x, y, color, bg_color, font);
        ofs_x += font->table[ch_index].width;
        string ++;
    }
}

void sgl_draw_font_string_len(sgl_surf_t *surf, const char *string, int len, int x, int y, sgl_color_t color, sgl_color_t bg_color, sgl_font_t *font)
{
    int ofs_x = x;
    int ch_index;
    while(len -- ){
        ch_index = (uint8_t)(*string) - 32;
        sgl_draw_font(surf, *string, ofs_x, y, color, bg_color, font);
        ofs_x += font->table[ch_index].width;
        string ++;
    }
}


void sgl_draw_font_transp(sgl_surf_t *surf, char ch, int x, int y, sgl_color_t color, sgl_color_t bg_color, sgl_font_t *font)
{
    int ch_index = (uint8_t)ch - 32;
    const uint8_t *dot = &font->bitmap[font->table[ch_index].bitmap_index];
    for(int i = 0; i < font->table[ch_index].height; i++) {
        for(int j = 0; j < font->table[ch_index].width; j +=2) {
            if((((*dot)>>4)&0xF)*16 != 0) {
                sgl_draw_point(surf, x + j + font->table[ch_index].ofs_x, y + i + font->table[ch_index].ofs_y, sgl_color_mixer(color, bg_color, (((*dot)>>4)&0xF)*16));
                sgl_draw_point(surf, x + j + font->table[ch_index].ofs_x + 1, y + i + font->table[ch_index].ofs_y, sgl_color_mixer(color, bg_color, (((*dot))&0xF)*16));
            }
            dot ++;
        }
    }
}

void sgl_draw_font_transp_string(sgl_surf_t *surf, const char *string, int x, int y, sgl_color_t color, sgl_color_t bg_color, sgl_font_t *font)
{
    int ofs_x = x;
    int ch_index;
    while(*string) {
        ch_index = (uint8_t)(*string) - 32;
        sgl_draw_font_transp(surf, *string, ofs_x, y, color, bg_color, font);
        ofs_x += font->table[ch_index].width;
        string ++;
    }
}

void sgl_draw_font_transp_string_len(sgl_surf_t *surf, const char *string, int len, int x, int y, sgl_color_t color, sgl_color_t bg_color, sgl_font_t *font)
{
    int ofs_x = x;
    int ch_index;
    while(len -- ){
        ch_index = (uint8_t)(*string) - 32;
        sgl_draw_font_transp(surf, *string, ofs_x, y, color, bg_color, font);
        ofs_x += font->table[ch_index].width;
        string ++;
    }
}